package org.game.db.bean;

import static org.game.db.StaticResource.MailResource.COLUMN_BASICTYPES;
import com.sojoys.artifact.factory.bytebuff.IByteBuff;
import org.apache.commons.lang3.ArrayUtils;
import static org.game.db.StaticResource.MailResource.FK_PINGYING_COLUMN_NAMES;
import static org.game.db.StaticResource.MailResource.*;
import java.sql.SQLException;
import com.sojoys.artifact.tools.ToolMap;
import static org.game.db.StaticResource.MailResource.COLUMN_COLUMNTYPES;
import com.sojoys.artifact.tools.SK_Plus;
import org.game.db.cache.PlayerCache;
import java.sql.ResultSet;
import java.util.Map;
import static org.game.db.StaticResource.MailResource.COLUMN_COUNT;
import static org.game.db.StaticResource.MailResource.SOURCE_COLUMN_NAMES;
import com.sojoys.artifact.constant.DataModelKeyEnum;
import static org.game.db.StaticResource.MailResource.HASHCODE_COLUMN_NAMES;
import static org.game.db.StaticResource.MailResource.FK_HASHCODE_COLUMN_NAMES;
import static org.game.db.StaticResource.MailResource.SOURCE_COLUMN_REMARKS;
import static org.game.db.StaticResource.MailResource.FK_SOURCE_COLUMN_NAMES;
import static org.game.db.StaticResource.MailResource.FK_SOURCE_TABLE_NAMES;
import static org.game.db.StaticResource.MailResource.FK_PINGYING_TABLE_NAMES;
import com.sojoys.artifact.build.data.base.BasePojo;
import static org.game.db.StaticResource.MailResource.PINGYING_COLUMN_NAMES;
import com.sojoys.artifact.tools.ToolCopy;
import org.game.db.cache.MailCache;
import com.alibaba.fastjson.JSONObject;
import static org.game.db.StaticResource.MailResource.COLUMN_CLASSTYPES;
import static org.game.db.StaticResource.MailResource.FK_HASHCODE_TABLE_NAMES;
import static org.game.db.StaticResource.MailResource.*;
/**
 * 邮件
 */
public class Mail extends BasePojo<Mail> implements org.game.db.vo.BaseMail{
	
	
	/**
	 * 索引值
	 */
	private String[] indexValues = new String[3];
	
	/**
	* id
	*/
	private long id;
	
	/**
	* 发送者
	*/
	private int senderId;
	
	/**
	* 收件人id
	*/
	private int playerId;
	
	/**
	* 文字类型
	*/
	private int textType;
	
	/**
	* 邮件标题
	*/
	private String title;
	
	/**
	* 邮件正文
	*/
	private String body;
	
	/**
	* 内容参数
	*/
	private Map bodyParam;
	
	/**
	* 附件
	*/
	private Map items;
	
	/**
	* 状态：0未读
	* 1已读
	* 2已收
	* 3已删
	*/
	private int status;
	
	/**
	* 附件来源
	*/
	private String dsp;
	
	/**
	* create_date
	*/
	private java.util.Date createDate;
	
	/**
	* modified_date
	*/
	private java.util.Date modifiedDate;
	
	
	@Deprecated
	public static Mail builder() {
		Mail mail = new Mail();
		return mail;
	}
	
	@Deprecated
	public Mail() {
		super();
	}
	
	public Mail(long id, int senderId, int playerId, int textType, String title, String body, Map bodyParam, Map items, int status, String dsp) {
		super();
		this.id = id;
		this.senderId = senderId;
		this.playerId = playerId;
		this.textType = textType;
		this.title = title;
		this.body = body;
		this.bodyParam = bodyParam;
		this.items = items;
		this.status = status;
		this.dsp = dsp;
		this.createDate = new java.util.Date();
		this.modifiedDate = new java.util.Date();
	}
	
	/**
	 * id
	 */
	public long getId() {return id;}
	
	/**
	 * id
	 */
	public Mail setId(long id) {this.id = id;return this;}
	
	/**
	 * 发送者
	 */
	public int getSenderId() {return senderId;}
	
	/**
	 * 发送者
	 */
	public Mail setSenderId(int senderId) {this.senderId = senderId;return this;}
	
	/**
	 * 收件人id
	 */
	public int getPlayerId() {return playerId;}
	
	/**
	 * 收件人id
	 */
	public Mail setPlayerId(int playerId) {this.playerId = playerId;return this;}
	
	/**
	 * 文字类型
	 */
	public int getTextType() {return textType;}
	
	/**
	 * 文字类型
	 */
	public Mail setTextType(int textType) {this.textType = textType;return this;}
	
	/**
	 * 邮件标题
	 */
	public String getTitle() {return title;}
	
	/**
	 * 邮件标题
	 */
	public Mail setTitle(String title) {this.title = title;return this;}
	
	/**
	 * 邮件正文
	 */
	public String getBody() {return body;}
	
	/**
	 * 邮件正文
	 */
	public Mail setBody(String body) {this.body = body;return this;}
	
	/**
	 * 内容参数
	 */
	public Map getBodyParam() {return bodyParam;}
	
	/**
	 * 内容参数
	 */
	public Mail setBodyParam(Map bodyParam) {this.bodyParam = bodyParam;return this;}
	
	/**
	 * 内容参数
	 */
	public String getBodyParamString() {
	    return toJSONString(bodyParam);
	}
	
	/**
	 * 内容参数
	 */
	public Map initBodyParamString(String str) {
		return forJsonObject(str,Map.class);
	}	
	/**
	 * 附件
	 */
	public Map getItems() {return items;}
	
	/**
	 * 附件
	 */
	public Mail setItems(Map items) {this.items = items;return this;}
	
	/**
	 * 附件
	 */
	public byte[] getItemsByte() {
		return toBytes(items);
	}
	
	/**
	 * 附件
	 */
	public Map initItemsByte(byte[] _byte) {
		return (Map)forBytesObject(_byte);
	}	
	/**
	 * 状态：0未读
	 * 1已读
	 * 2已收
	 * 3已删
	 */
	public int getStatus() {return status;}
	
	/**
	 * 状态：0未读
	 * 1已读
	 * 2已收
	 * 3已删
	 */
	public Mail setStatus(int status) {this.status = status;return this;}
	
	/**
	 * 附件来源
	 */
	public String getDsp() {return dsp;}
	
	/**
	 * 附件来源
	 */
	public Mail setDsp(String dsp) {this.dsp = dsp;return this;}
	
	/**
	 * create_date
	 */
	public java.util.Date getCreateDate() {return createDate;}
	
	/**
	 * create_date
	 */
	protected Mail setCreateDate(java.util.Date createDate) {this.createDate = createDate;return this;}
	
	/**
	 * modified_date
	 */
	public java.util.Date getModifiedDate() {return modifiedDate;}
	
	/**
	 * modified_date
	 */
	protected Mail setModifiedDate(java.util.Date modifiedDate) {this.modifiedDate = modifiedDate;return this;}
	
	
	// 代理
	
	protected Object[] getColumnNames(DataModelKeyEnum e){
		switch (e) {
		case SOURCE:
			return SOURCE_COLUMN_NAMES;
		case PINGYING:
			return PINGYING_COLUMN_NAMES;
		case HASHCODE:
			return HASHCODE_COLUMN_NAMES;
		case REMARKS:
			return SOURCE_COLUMN_REMARKS;
		default:
			return PINGYING_COLUMN_NAMES;
		}
	}
	
	protected Object[] getFKTableNames(DataModelKeyEnum e){
		switch (e) {
		case SOURCE:
			return FK_SOURCE_TABLE_NAMES;
		case PINGYING:
			return FK_PINGYING_TABLE_NAMES;
		case HASHCODE:
			return FK_HASHCODE_TABLE_NAMES;
		default:
			return FK_PINGYING_TABLE_NAMES;
		}
	}
	
	protected Object[] getFKColumnNames(DataModelKeyEnum e){
		switch (e) {
		case SOURCE:
			return FK_SOURCE_COLUMN_NAMES;
		case PINGYING:
			return FK_PINGYING_COLUMN_NAMES;
		case HASHCODE:
			return FK_HASHCODE_COLUMN_NAMES;
		default:
			return FK_PINGYING_TABLE_NAMES;
		}
	}
	
	protected String[] getBasicTypes(){
		return COLUMN_BASICTYPES;
	}
	
	protected Class[] getClassTypes(){
		return COLUMN_CLASSTYPES;
	}
	
	protected String[] getColumnTypes(){
		return COLUMN_COLUMNTYPES;
	}
	
	protected int getColumnCount(){
		return COLUMN_COUNT;
	}
	
	protected Object[] getColumeValues(){
		Object[] values = new Object[COLUMN_COUNT];
        values[0] = this.id;
        values[1] = this.senderId;
        values[2] = this.playerId;
        values[3] = this.textType;
        values[4] = this.title;
        values[5] = this.body;
        values[6] = this.bodyParam;
        values[7] = this.items;
        values[8] = this.status;
        values[9] = this.dsp;
        values[10] = this.createDate;
        values[11] = this.modifiedDate;
        return values;
	}
	
	public Mail deepCopy(){
		Mail mail = null;
		try {
			mail = (Mail) super.clone();
		    mail.setBodyParam(ToolCopy.copyObject(this.bodyParam));
		    mail.setItems(ToolCopy.copyObject(this.items));
		    mail.setCreateDate(ToolCopy.copyObject(this.createDate));
		    mail.setModifiedDate(ToolCopy.copyObject(this.modifiedDate));
		    // 克隆索引
		    mail.indexValues = ArrayUtils.clone(indexValues);
			return mail;
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return mail;
	}
	
	
	protected Map<Object, Object> toMap(Object[] keys,Map<Object, Object> map){
        map.put(keys[0], this.id);
        map.put(keys[1], this.senderId);
        map.put(keys[2], this.playerId);
        map.put(keys[3], this.textType);
        map.put(keys[4], this.title);
        map.put(keys[5], this.body);
        map.put(keys[6], this.bodyParam);
        map.put(keys[7], this.items);
        map.put(keys[8], this.status);
        map.put(keys[9], this.dsp);
        map.put(keys[10], this.createDate);
        map.put(keys[11], this.modifiedDate);
        return map;
    }
    
    protected Mail createForMap(Object[] keys,Map map){
   		this.id = (long)map.get(keys[0]);
   		this.senderId = (int)map.get(keys[1]);
   		this.playerId = (int)map.get(keys[2]);
   		this.textType = (int)map.get(keys[3]);
   		this.title = (String)map.get(keys[4]);
   		this.body = (String)map.get(keys[5]);
   		this.bodyParam = (Map)map.get(keys[6]);
   		this.items = (Map)map.get(keys[7]);
   		this.status = (int)map.get(keys[8]);
   		this.dsp = (String)map.get(keys[9]);
   		this.createDate = (java.util.Date)map.get(keys[10]);
   		this.modifiedDate = (java.util.Date)map.get(keys[11]);
        return this;
    }
	
	
	protected JSONObject toJSONObject(Object[] keys,JSONObject obj){
        obj.put(keys[0], this.id);
        obj.put(keys[1], this.senderId);
        obj.put(keys[2], this.playerId);
        obj.put(keys[3], this.textType);
        obj.put(keys[4], this.title);
        obj.put(keys[5], this.body);
        obj.put(keys[6], this.bodyParam);
        obj.put(keys[7], toBytes(items));
        obj.put(keys[8], this.status);
        obj.put(keys[9], this.dsp);
        obj.put(keys[10], this.createDate);
        obj.put(keys[11], this.modifiedDate);
        return obj;
    }
    
    protected Mail createForJSONObject(Object[] keys,JSONObject obj){
	    this.id = obj.getLongValue(keys[0]);
	    this.senderId = obj.getIntValue(keys[1]);
	    this.playerId = obj.getIntValue(keys[2]);
	    this.textType = obj.getIntValue(keys[3]);
	    this.title = obj.getString(keys[4]);
	    this.body = obj.getString(keys[5]);
		this.bodyParam = obj.getObject(keys[6],Map.class);
		this.items = (Map)forBytesObject(obj.getBytes(keys[7]));
	    this.status = obj.getIntValue(keys[8]);
	    this.dsp = obj.getString(keys[9]);
	    this.createDate = obj.getDate(keys[10]);
	    this.modifiedDate = obj.getDate(keys[11]);
        return this;
    }
    
    
    public Mail createForResultSet(ResultSet rs) throws SQLException{
	    this.id = rs.getLong(SOURCE_COLUMN_NAMES[0]);
	    this.senderId = rs.getInt(SOURCE_COLUMN_NAMES[1]);
	    this.playerId = rs.getInt(SOURCE_COLUMN_NAMES[2]);
	    this.textType = rs.getInt(SOURCE_COLUMN_NAMES[3]);
	    this.title = rs.getString(SOURCE_COLUMN_NAMES[4]);
	    this.body = rs.getString(SOURCE_COLUMN_NAMES[5]);
		this.bodyParam = forJsonObject(rs.getString(SOURCE_COLUMN_NAMES[6]),Map.class);
		this.items = (Map)forBytesObject(rs.getBytes(SOURCE_COLUMN_NAMES[7]));
	    this.status = rs.getInt(SOURCE_COLUMN_NAMES[8]);
	    this.dsp = rs.getString(SOURCE_COLUMN_NAMES[9]);
	    this.createDate = rs.getTimestamp(SOURCE_COLUMN_NAMES[10]);
	    this.modifiedDate = rs.getTimestamp(SOURCE_COLUMN_NAMES[11]);
	    return this;
    }
    
    protected void writeBytes(IByteBuff byteBuf){
	    byteBuf.writeLong(this.id);
	    byteBuf.writeInt(this.senderId);
	    byteBuf.writeInt(this.playerId);
	    byteBuf.writeInt(this.textType);
	    byteBuf.writeString(this.title);
	    byteBuf.writeString(this.body);
	    byteBuf.writeObject(this.bodyParam);
	    byteBuf.writeObject(this.items);
	    byteBuf.writeInt(this.status);
	    byteBuf.writeString(this.dsp);
	    byteBuf.writeDate(this.createDate);
	    byteBuf.writeDate(this.modifiedDate);
    }
	
	protected Mail readBytes(IByteBuff byteBuf){
	    this.id = byteBuf.readLong(null);
	    this.senderId = byteBuf.readInt(null);
	    this.playerId = byteBuf.readInt(null);
	    this.textType = byteBuf.readInt(null);
	    this.title = byteBuf.readString(null);
	    this.body = byteBuf.readString(null);
	    this.bodyParam = (Map)byteBuf.readObject();
	    this.items = (Map)byteBuf.readObject();
	    this.status = byteBuf.readInt(null);
	    this.dsp = byteBuf.readString(null);
	    this.createDate = byteBuf.readDate(null);
	    this.modifiedDate = byteBuf.readDate(null);
	    return this;
	}
    
    
    protected JSONObject exportFKTableMap(Object[] keys,JSONObject map,DataModelKeyEnum e){
        return map;
    }
    
    protected boolean importFKTableMap(Object[] keys,JSONObject map,DataModelKeyEnum e){
		return true;
    }
    
	/** 取得[player]*/
	public Player getPlayerPkPlayerId(){
		return PlayerCache.me().getById(playerId);
	}
	
    /** 级联删除(延迟入库) */
    public boolean deleteAndSon(){
    	return false;
    }
    
    /** 级联删除(及时入库) */
    public boolean deleteAndSonAndFlush(){
    	return false;
    }
    
    /** 延迟插入数据库 */
    public Mail insert(){
    	return MailCache.me().insert(this);
    }
    /** 延迟更新数据库 */
    public Mail update(){
    	return MailCache.me().update(this);
    }
    /** 延迟删除数据库 */
    public boolean delete(){
    	return MailCache.me().delete(this);
    }
    /** 即时插入数据库 */
    public Mail insertAndFlush(){
    	return MailCache.me().insertAndFlush(this);
    }
    /** 即时更新数据库 */
    public Mail updateAndFlush(){
    	return MailCache.me().updateAndFlush(this);
    }
    /** 即时删除数据库 */
    public boolean deleteAndFlush(){
    	return MailCache.me().deleteAndFlush(this);
    }
    
    
	
	/**
	 * 同步索引值
	 */
	public Mail syncIndexValues(){
		indexValues[0] = SK_Plus.b(getId()).e();
		indexValues[1] = SK_Plus.b(getPlayerId()).e();
		indexValues[2] = SK_Plus.b(getPlayerId(),"_",getStatus()).e();
		return this;
	}
	/**
	 * 取得索引
	 */
	public String[] getIndexValues(){
		return indexValues;
	}
	
	protected Object getPrimaryKey(){
		return getId();
	}
	
	protected void setPrimaryKey(Object key){
		setId((long)key);
	}
//自定义内容起始位置
//自定义内容结束位置
}